package com.paic.mhis.hcpms.agreement.protocol.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.dto.BaseDTO;
import com.paic.mhis.common.base.dto.PaginatedQueryDTO;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.common.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.AttendantProviderService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HealthCareConstants;
import com.paic.mhis.hcpms.agreement.protocol.biz.service.AgreementService;
import com.paic.mhis.hcpms.agreement.protocol.biz.validator.CapitationPayValidator;
import com.paic.mhis.hcpms.agreement.protocol.biz.validator.DiseasePayValidator;
import com.paic.mhis.hcpms.agreement.protocol.biz.validator.GlogalBudgetValidator;
import com.paic.mhis.hcpms.agreement.protocol.biz.validator.PreAuthorizationValidator;
import com.paic.mhis.hcpms.agreement.protocol.biz.validator.ServiceItemPayValidator;
import com.paic.mhis.hcpms.agreement.protocol.dto.CapitationPayDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.DiseasePayDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.DiseasePayPaginationDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.GlobalBudgetDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.PreAuthorizationDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.PreAuthorizationPaginationDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.ServiceItemPayDTO;
import com.paic.mhis.hcpms.agreement.protocol.dto.ServiceItemPayPaginationDTO;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementConstants;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementServiceUtil;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementSqlMapIdConstants;
import com.paic.pafa.app.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

/**
 * Description: 协议管理模块实现 hcpms_j2ee EX-LUORONG001 创建
 */
@Component("agreementService")
public class AgreementServiceImpl extends BaseService implements
		AgreementService {
	@Autowired
	private CommonDAO commonDao;

	private AgreementServiceUtil agreementServiceUtil;

	private GlogalBudgetValidator budgetValidator;

	private CapitationPayValidator capitationPayValidator;

	private ServiceItemPayValidator itemPayValidator;

	private DiseasePayValidator diseasePayValidator;

	private PreAuthorizationValidator authorizationValidator;

	@Autowired
	private AttendantProviderService attendantProviderService;

	private static final Logger logger = Logger
			.getLogger(AgreementServiceImpl.class);

	/**
	 * 协议信息新增
	 */
	@Override
	@Transactional
	public Map<String, Object> add(JSONObject transferJson, String currentUser)
			throws BusinessServiceException {
		agreementServiceUtil = (AgreementServiceUtil) context
				.getBean("agreementServiceUtil");
		return agreementServiceUtil.add(transferJson, currentUser);
	}

	/**
	 * 文件信息新增
	 */
	@Override
	@Transactional
	public Map<String, Object> addImage(JSONObject transferJson,
			String currentUser, String fileName, long fileSize)
			throws BusinessServiceException {
		try {
			agreementServiceUtil = (AgreementServiceUtil) context
					.getBean("agreementServiceUtil");

		} catch (Exception e) {
			logger.info(e);
		}
		return agreementServiceUtil.addImage(transferJson, currentUser,
				fileName, fileSize);

	}

	/**
	 * 已维护协议信息查询（分页＄1�7
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> queryPage(int pageSize, int page,
			Map<String, Object> params) throws BusinessServiceException {

		String userName = (String) params.get("userName");
		List<Map<String, String>> userTypeList = attendantProviderService
				.getUserType(userName);
		String userType = "";
		String orgCode = "";
		if (null != userTypeList) {
			for (int i = 0; i < userTypeList.size(); i++) {
				Map<String, String> tempMap = userTypeList.get(i);
				userType = (String) tempMap.get("accountType");
				orgCode = orgCode + "'" + (String) tempMap.get("orgCode")
						+ "',";
			}
		}
		// 医院类型账户
		if (HealthCareConstants.USER_HOSPITAL.equals(userType)) {
			orgCode = orgCode.substring(0, orgCode.length() - 1);
			params.put("orgCode", orgCode);
		}
		PaginatedQueryDTO dto = new PaginatedQueryDTO();
		dto.setStart(pageSize);
		dto.setLimit(page);
		dto.setParam(params);
		return commonDao.queryPage(
				AgreementSqlMapIdConstants.AGREEMENT_QUERYPAGE, dto);
	}

	/**
	 * 已维护协议详惄1�7
	 */
	@Override
	public Map<String, Object> get(String id) throws BusinessServiceException {
		agreementServiceUtil = (AgreementServiceUtil) context
				.getBean("agreementServiceUtil");
		return agreementServiceUtil.get(id);

	}

	/**
	 * 查询文件list
	 */
	@Override
	public Map<String, Object> getImageList(String id)
			throws BusinessServiceException {
		agreementServiceUtil = (AgreementServiceUtil) context
				.getBean("agreementServiceUtil");
		return agreementServiceUtil.getImage(id);

	}

	/**
	 * 协议信息修改
	 */
	@Override
	@Transactional
	public Map<String, Object> update(JSONObject transferJson,
			String currentUser) throws BusinessServiceException {
		agreementServiceUtil = (AgreementServiceUtil) context
				.getBean("agreementServiceUtil");
		return agreementServiceUtil.update(transferJson, currentUser);
	}

	/**
	 * 协议信息修改状�1�7�1�7
	 */
	@Override
	public Map<String, Object> delete(JSONObject transferJson)
			throws BusinessServiceException {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			// 删除时，验证是否该协议下是否存在支付方式
			// 存在：提示是否继续删除；
			// id
			String id = transferJson.getString("id");
			String ids = id.substring(0, id.length() - 1);
			paramsMap.put("id", ids);
			// 是否有效[0-有效＄1�7无效]
			paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
			paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
			// 协议逻辑删除
			this.commonDao.update(AgreementSqlMapIdConstants.AGREEMENT_DELETE,
					paramsMap);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_AGREEMENT_INFO_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 文件删除
	 */
	@Override
	public Map<String, Object> deleteFile(JSONObject transferJson)
			throws BusinessServiceException {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			// 删除时，验证是否该协议下是否存在支付方式
			// 存在：提示是否继续删除；
			// id
			String id = transferJson.getString("id");
			paramsMap.put("id", id);
			// 是否有效[0-有效＄1�7无效]
			paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
			paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
			// 协议逻辑删除
			this.commonDao.update(AgreementSqlMapIdConstants.FILE_DELETE,
					paramsMap);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_AGREEMENT_INFO_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按�1�7�额付费查询
	 */
	@Override
	public Map<String, Object> queryGlobalBudget(String agreementId)
			throws BusinessServiceException {
		// 查询按�1�7�额付费
		List<GlobalBudgetDTO> list;
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			paramsMap.put("agreementId", agreementId);
			list = commonDao.query(
					AgreementSqlMapIdConstants.GLOBALBUDGET_QUERY, paramsMap);

			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按�1�7�额付费详细信息
	 */
	@Override
	public Map<String, Object> getGlobalBudget(String id)
			throws BusinessServiceException {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> results = null;

		try {
			paramsMap.put("id", id);
			results = (Map<String, Object>) commonDao.get(
					AgreementSqlMapIdConstants.GLOBALBUDGET_GET_BY_NO,
					paramsMap);

			result.put(Constants.SUCCESS_RESULT_KEY, results);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按�1�7�额付费新增
	 */
	@Override
	@Transactional
	public Map<String, Object> addGlobalBudget(JSONObject transferJson)
			throws BusinessServiceException {
		GlobalBudgetDTO infoDTO;
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> tempParam = new HashMap<String, Object>();
		Map<String, Object> agreeInfoDTO = null;
		List<BaseDTO> list = null;
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			budgetValidator = (GlogalBudgetValidator) context
					.getBean("glogalBudgetValidator");
			Map<Object, Object> validateResult = budgetValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						GlobalBudgetDTO.class);
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				String codetype = AgreementConstants.RATIOTYPE;
				tempParam.put("codeType", codetype);
				tempParam.put("codeNo", infoDTO.getRatioType());

				list = commonDao.query("base.codeTypes", tempParam);
				if (list.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"比例类型不存在!");
					return result;
				}
				tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				agreeInfoDTO = (Map<String, Object>) commonDao.get(
						AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
						tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}

				this.commonDao
						.insert(AgreementSqlMapIdConstants.GLOBALBUDGET_INSERT,
								infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.ADD_GLOBALBUDGET_SUCCESS);

			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));
			}

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按�1�7�额付费修改
	 */
	@Override
	@Transactional
	public Map<String, Object> updateGlobalBudget(JSONObject transferJson)
			throws BusinessServiceException {
		GlobalBudgetDTO infoDTO;
		List<BaseDTO> list = null;
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> tempParam = new HashMap<String, Object>();

		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			budgetValidator = (GlogalBudgetValidator) context
					.getBean("glogalBudgetValidator");
			Map<Object, Object> validateResult = budgetValidator
					.validate(fieldMap);
			if (null == validateResult) {

				infoDTO = JSONObject.toJavaObject(transferJson,
						GlobalBudgetDTO.class);
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				String codetype = AgreementConstants.RATIOTYPE;
				tempParam.put("codeType", codetype);
				tempParam.put("codeNo", infoDTO.getRatioType());

				list = commonDao.query("base.codeTypes", tempParam);
				if (list == null || list.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"比例类型不存在!");
					return result;
				}
				tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao
						.update(AgreementSqlMapIdConstants.GLOBALBUDGET_UPDATE,
								infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.UPDATE_GLOBALBUDGET_SUCCESS);

			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));
			}

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按�1�7�额付费修改状�1�7�1�7
	 */
	@Override
	public Map<String, Object> deleteGlobalBudget(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String id = transferJson.getString("id");

			String[] idArray = id.split(",");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < idArray.length; i++) {
				sb.append("'" + idArray[i] + "',");
			}
			String ids = "";
			if (!"".equals(sb.toString())) {
				ids = sb.toString()
						.substring(0, sb.toString().lastIndexOf(','));
				paramsMap.put("id", ids);
				// 是否有效[0-有效＄1�7无效]
				paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
				paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
				paramsMap.put("dateUpdated",
						transferJson.getString("dateUpdated"));

				this.commonDao.update(
						AgreementSqlMapIdConstants.GLOBALBUDGET_DELETE,
						paramsMap);
			}
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_GLOBALBUDGET_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按人头付费查评1�7
	 */
	@Override
	public Map<String, Object> queryCapitationPay(String agreementId)
			throws BusinessServiceException {
		// 按人头付费查评1�7
		List<CapitationPayDTO> list;
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			paramsMap.put("agreementId", agreementId);
			list = commonDao.query(
					AgreementSqlMapIdConstants.CAPITATIONPAY_QUERY, paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按人头付费详细信恄1�7
	 */
	@Override
	public Map<String, Object> getCapitationPay(String id)
			throws BusinessServiceException {
		CapitationPayDTO infoDTO;
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			// 服务协议类型
			paramsMap.put("id", id);
			infoDTO = (CapitationPayDTO) commonDao.get(
					AgreementSqlMapIdConstants.CAPITATIONPAY_GET_BY_NO,
					paramsMap);

			result.put(Constants.SUCCESS_RESULT_KEY, infoDTO);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按人头付费新墄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> addCapitationPay(JSONObject transferJson)
			throws BusinessServiceException {
		CapitationPayDTO infoDTO;
		List<BaseDTO> list = null;
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> tempParam = new HashMap<String, Object>();
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			capitationPayValidator = (CapitationPayValidator) context
					.getBean("capitationPayValidator");
			Map<Object, Object> validateResult = capitationPayValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						CapitationPayDTO.class);
				// 是否有效[0-有效＄1�7无效]
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				String codetype = AgreementConstants.DIAGTYPE;
				tempParam.put("codeType", codetype);
				tempParam.put("codeNo", infoDTO.getAttendanceType());

				list = commonDao.query("base.codeTypes", tempParam);
				if (list == null || list.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"就诊类型不存在!");
					return result;
				}
				tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.insert(
						AgreementSqlMapIdConstants.CAPITATIONPAY_INSERT,
						infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.ADD_CAPITATIONPAY_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));
			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按人头付费修攄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> updateCapitationPay(JSONObject transferJson)
			throws BusinessServiceException {
		CapitationPayDTO infoDTO;
		List<BaseDTO> list = null;
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> tempParam = new HashMap<String, Object>();
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			capitationPayValidator = (CapitationPayValidator) context
					.getBean("capitationPayValidator");
			Map<Object, Object> validateResult = capitationPayValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						CapitationPayDTO.class);
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				String codetype = AgreementConstants.DIAGTYPE;
				tempParam.put("codeType", codetype);
				tempParam.put("codeNo", infoDTO.getAttendanceType());

				list = commonDao.query("base.codeTypes", tempParam);
				if (list == null || list.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"就诊类型不存在!");
					return result;
				}
				tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.update(
						AgreementSqlMapIdConstants.CAPITATIONPAY_UPDATE,
						infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.UPDATE_CAPITATIONPAY_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按人头付费修改状怄1�7
	 */
	@Override
	public Map<String, Object> deleteCapitationPay(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String id = transferJson.getString("id");

			String[] idArray = id.split(",");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < idArray.length; i++) {
				sb.append("'" + idArray[i] + "',");
			}
			String ids = "";
			if (!"".equals(sb.toString())) {
				ids = sb.toString()
						.substring(0, sb.toString().lastIndexOf(','));
				paramsMap.put("id", ids);
				// 是否有效[0-有效＄1�7无效]
				paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
				paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
				paramsMap.put("dateUpdated",
						transferJson.getString("dateUpdated"));
				this.commonDao.update(
						AgreementSqlMapIdConstants.CAPITATIONPAY_DELETE,
						paramsMap);
			}
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_CAPITATIONPAY_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按项目付费查询（分页＄1�7
	 */
	@Override
	public Map<String, Object> queryPageServiceItemPay(String pageSize,
			String page, String agreementId) throws BusinessServiceException {
		ServiceItemPayPaginationDTO serviceItemPayPaginationDTO = new ServiceItemPayPaginationDTO();
		Map<String, Object> resultMap = null;
		Map<Object, Object> paramsMap = new HashMap<Object, Object>();

		try {
			serviceItemPayPaginationDTO.setPage(Integer.valueOf(page));
			serviceItemPayPaginationDTO.setPageSize(Integer.valueOf(pageSize));
			paramsMap.put("agreementId", agreementId);
			serviceItemPayPaginationDTO.setQueryParams(paramsMap);

			serviceItemPayPaginationDTO.setDoCount(true);
			serviceItemPayPaginationDTO.setQueryAll(false);
			resultMap = commonDao.queryPage(
					AgreementSqlMapIdConstants.SERVICEITEMPAY_QUERYPAGE,
					serviceItemPayPaginationDTO);

			generateSuccessResult(resultMap, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return resultMap;
	}

	/**
	 * 按项目付费详细信恄1�7
	 */
	@Override
	public Map<String, Object> getServiceItemPay(String id)
			throws BusinessServiceException {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> results = null;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 服务协议类型
			paramsMap.put("id", id);
			results = (Map<String, Object>) commonDao.get(
					AgreementSqlMapIdConstants.SERVICEITEMPAY_GET_BY_NO,
					paramsMap);

			result.put(Constants.SUCCESS_RESULT_KEY, results);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按项目付费新墄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> addServiceItemPay(JSONObject transferJson)
			throws BusinessServiceException {
		ServiceItemPayDTO infoDTO;

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> tempParam = null;
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			itemPayValidator = (ServiceItemPayValidator) context
					.getBean("serviceItemPayValidator");
			Map<Object, Object> validateResult = itemPayValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						ServiceItemPayDTO.class);
				// 是否有效[0-有效＄1�7无效]
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);

				tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.insert(
						AgreementSqlMapIdConstants.SERVICEITEMPAY_INSERT,
						infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.ADD_SERVICEITEMPAY_SUCCESS);

			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按项目付费修攄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> updateServiceItemPay(JSONObject transferJson)
			throws BusinessServiceException {
		ServiceItemPayDTO infoDTO;
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			itemPayValidator = (ServiceItemPayValidator) context
					.getBean("serviceItemPayValidator");
			Map<Object, Object> validateResult = itemPayValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						ServiceItemPayDTO.class);
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				Map<String, Object> tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.update(
						AgreementSqlMapIdConstants.SERVICEITEMPAY_UPDATE,
						infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.UPDATE_SERVICEITEMPAY_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}
		} catch (Exception e) {
			throw new BusinessServiceException(Constants.FAILED_STATE_VALUE, e);
		}
		return result;
	}

	/**
	 * 按项目付费修改状怄1�7
	 */
	@Override
	public Map<String, Object> deleteServiceItemPay(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();

		try {
			String id = transferJson.getString("id");

			String[] idArray = id.split(",");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < idArray.length; i++) {
				sb.append("'" + idArray[i] + "',");
			}
			String ids = "";
			if (!"".equals(sb.toString())) {
				ids = sb.toString()
						.substring(0, sb.toString().lastIndexOf(','));
				paramsMap.put("id", ids);
				// 是否有效[0-有效＄1�7无效]
				paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
				paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
				paramsMap.put("dateUpdated",
						transferJson.getString("dateUpdated"));
				this.commonDao.update(
						AgreementSqlMapIdConstants.SERVICEITEMPAY_DELETE,
						paramsMap);
			}
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_SERVICEITEMPAY_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按病种付费查询（分页＄1�7
	 */
	@Override
	public Map<String, Object> queryPageDiseasePay(String pageSize,
			String page, String agreementId) throws BusinessServiceException {
		DiseasePayPaginationDTO diseasePayPaginationDTO = new DiseasePayPaginationDTO();
		Map<String, Object> resultMap = null;

		try {
			Map<Object, Object> paramsMap = new HashMap<Object, Object>();

			diseasePayPaginationDTO.setPage(Integer.valueOf(page));
			diseasePayPaginationDTO.setPageSize(Integer.valueOf(pageSize));
			paramsMap.put("agreementId", agreementId);
			diseasePayPaginationDTO.setQueryParams(paramsMap);

			diseasePayPaginationDTO.setDoCount(true);
			diseasePayPaginationDTO.setQueryAll(false);
			resultMap = commonDao.queryPage(
					AgreementSqlMapIdConstants.DISEASEPAY_QUERYPAGE,
					diseasePayPaginationDTO);

			generateSuccessResult(resultMap, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return resultMap;
	}

	/**
	 * 按病种付费详细信恄1�7
	 */
	@Override
	public Map<String, Object> getDiseasePay(String id)
			throws BusinessServiceException {
		DiseasePayDTO infoDTO;
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 服务协议类型
			paramsMap.put("id", id);
			infoDTO = (DiseasePayDTO) commonDao.get(
					AgreementSqlMapIdConstants.DISEASEPAY_GET_BY_NO, paramsMap);

			result.put(Constants.SUCCESS_RESULT_KEY, infoDTO);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按病种付费新墄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> addDiseasePay(JSONObject transferJson)
			throws BusinessServiceException {
		DiseasePayDTO infoDTO;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			diseasePayValidator = (DiseasePayValidator) context
					.getBean("diseasePayValidator");
			Map<Object, Object> validateResult = diseasePayValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						DiseasePayDTO.class);
				// 是否有效[0-有效＄1�7无效]
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				Map<String, Object> tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.insert(
						AgreementSqlMapIdConstants.DISEASEPAY_INSERT, infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.ADD_DISEASEPAY_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按病种付费修攄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> updateDiseasePay(JSONObject transferJson)
			throws BusinessServiceException {
		DiseasePayDTO infoDTO;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			diseasePayValidator = (DiseasePayValidator) context
					.getBean("diseasePayValidator");
			Map<Object, Object> validateResult = diseasePayValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						DiseasePayDTO.class);
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				Map<String, Object> tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.update(
						AgreementSqlMapIdConstants.DISEASEPAY_UPDATE, infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.UPDATE_DISEASEPAY_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 按病种付费修改状怄1�7
	 */
	@Override
	public Map<String, Object> deleteDiseasePay(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String id = transferJson.getString("id");

			String[] idArray = id.split(",");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < idArray.length; i++) {
				sb.append("'" + idArray[i] + "',");
			}
			String ids = "";
			if (!"".equals(sb.toString())) {
				ids = sb.toString()
						.substring(0, sb.toString().lastIndexOf(','));
				paramsMap.put("id", ids);
				// 是否有效[0-有效＄1�7无效]
				paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
				paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
				paramsMap.put("dateUpdated",
						transferJson.getString("dateUpdated"));
				this.commonDao
						.update(AgreementSqlMapIdConstants.DISEASEPAY_DELETE,
								paramsMap);
			}
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_DISEASEPAY_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 预授权方案查询（分页＄1�7
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> queryPagePreAuthorization(String pageSize,
			String page, String agreementId, String itemType, String itemCode,
			String itemName) throws BusinessServiceException {
		PreAuthorizationPaginationDTO preAuthorizationPaginationDTO = new PreAuthorizationPaginationDTO();
		Map<Object, Object> paramsMap = new HashMap<Object, Object>();
		Map<String, Object> resultMap = null;

		try {
			preAuthorizationPaginationDTO.setPage(Integer.valueOf(page));
			preAuthorizationPaginationDTO
					.setPageSize(Integer.valueOf(pageSize));
			paramsMap.put("agreementId", agreementId);
			paramsMap.put("itemType", itemType);
			paramsMap.put("itemCode", itemCode);
			paramsMap.put("itemName", itemName);
			preAuthorizationPaginationDTO.setQueryParams(paramsMap);
			preAuthorizationPaginationDTO.setDoCount(true);
			preAuthorizationPaginationDTO.setQueryAll(false);
			resultMap = commonDao.queryPage(
					AgreementSqlMapIdConstants.PREAUTHORIZATION_QUERYPAGE,
					preAuthorizationPaginationDTO);

			generateSuccessResult(resultMap, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return resultMap;
	}

	/**
	 * 预授权方案详细信恄1�7
	 */
	@Override
	public Map<String, Object> getPreAuthorization(String id)
			throws BusinessServiceException {
		PreAuthorizationDTO infoDTO;
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 服务协议类型
			paramsMap.put("id", id);
			infoDTO = (PreAuthorizationDTO) commonDao.get(
					AgreementSqlMapIdConstants.PREAUTHORIZATION_GET_BY_NO,
					paramsMap);

			result.put(Constants.SUCCESS_RESULT_KEY, infoDTO);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					Constants.SELECT_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 预授权方案新墄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> addPreAuthorization(JSONObject transferJson)
			throws BusinessServiceException {
		PreAuthorizationDTO infoDTO;
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> tempParam = new HashMap<String, Object>();
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			authorizationValidator = (PreAuthorizationValidator) context
					.getBean("preAuthorizationValidator");
			Map<Object, Object> validateResult = authorizationValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						PreAuthorizationDTO.class);
				// 是否有效[0-有效＄1�7无效]
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.insert(
						AgreementSqlMapIdConstants.PREAUTHORIZATION_INSERT,
						infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.ADD_PREAUTHORIZATION_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 预授权方案修攄1�7
	 */
	@Override
	@Transactional
	public Map<String, Object> updatePreAuthorization(JSONObject transferJson)
			throws BusinessServiceException {
		PreAuthorizationDTO infoDTO;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<Object, Object> fieldMap = JSONObject.toJavaObject(
					transferJson, Map.class);
			authorizationValidator = (PreAuthorizationValidator) context
					.getBean("preAuthorizationValidator");
			Map<Object, Object> validateResult = authorizationValidator
					.validate(fieldMap);
			if (null == validateResult) {
				infoDTO = JSONObject.toJavaObject(transferJson,
						PreAuthorizationDTO.class);
				infoDTO.setIsValid(AgreementConstants.IS_VALID_TRUE);
				Map<String, Object> tempParam = new HashMap<String, Object>();
				tempParam.put("id", fieldMap.get("agreementId"));
				Map<String, Object> agreeInfoDTO = (Map<String, Object>) commonDao
						.get(AgreementSqlMapIdConstants.AGREEMENT_GET_BY_NO,
								tempParam);
				if (null == agreeInfoDTO || agreeInfoDTO.size() == 0) {
					generateSuccessResult(result, Constants.FAILED_STATE_VALUE,
							"该协议已被其他用户删除，请核实");
					return result;
				}
				this.commonDao.update(
						AgreementSqlMapIdConstants.PREAUTHORIZATION_UPDATE,
						infoDTO);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
						AgreementConstants.UPDATE_PREAUTHORIZATION_SUCCESS);
			} else {
				BusinessServiceUtil.generateExceptionResult(result,
						Constants.STATE_KEY,
						Constants.FAILED_VALIDATE_STATE_VALUE,
						Constants.VALIDATE_ERRORS_KEY, BusinessServiceUtil
								.getValidateFailedMsg(validateResult));

			}

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 预授权方案修改状怄1�7
	 */
	@Override
	public Map<String, Object> deletePreAuthorization(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String id = transferJson.getString("id");

			String[] idArray = id.split(",");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < idArray.length; i++) {
				sb.append("'" + idArray[i] + "',");
			}
			String ids = "";
			if (!"".equals(sb.toString())) {
				ids = sb.toString()
						.substring(0, sb.toString().lastIndexOf(','));
				paramsMap.put("id", ids);
				// 是否有效[0-有效＄1�7无效]
				paramsMap.put("isValid", AgreementConstants.IS_VALID_FALSE);
				paramsMap.put("updatedBy", transferJson.getString("updatedBy"));
				paramsMap.put("dateUpdated",
						transferJson.getString("dateUpdated"));
				this.commonDao.update(
						AgreementSqlMapIdConstants.PREAUTHORIZATION_DELETE,
						paramsMap);
			}
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE,
					AgreementConstants.DELETE_PREAUTHORIZATION_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private void generateSuccessResult(Map<String, Object> result,
			String stateValue, String msgValue) {
		result.put(Constants.STATE_KEY, stateValue);
		result.put(Constants.MSG_KEY, msgValue);
	} // 同一医疗机构、同一服务类型的协议，生效、终止时间不能交叉

	@Override
	public List<Map<String, String>> getModePay() {

		return this.commonDao.query("hcpms.agreement.queryPaymentModel", null);
	}

}
